package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManagerService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: lx
 * @Time: 15:54
 * @Description:  后台管理中心
 *
 *
 */
@RestController
//@CrossOrigin
@RequestMapping("admin/product/")
public class ManagerController {
    @Autowired
    private ManagerService managerService;
    //查询一级分类
    @GetMapping("/getCategory1")
    public Result getCategory1(){
        List<BaseCategory1> list = managerService.getCategory1();
        return Result.ok(list);
    }
    //查询 二级分类
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2(@PathVariable(name = "category1Id") Long category1Id){
        List<BaseCategory2> list = managerService.getCategory2(category1Id);
        return Result.ok(list);
    }
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3(@PathVariable(name = "category2Id") Long category2Id){

        List<BaseCategory3> list = managerService.getCategory3(category2Id);
        return Result.ok(list);
    }
    //根据一二三级分类ID 查询 平台属性集合
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable(name = "category1Id") Long category1Id,
            @PathVariable(name = "category2Id") Long category2Id,
            @PathVariable(name = "category3Id") Long category3Id
    ){
        List<BaseAttrInfo> baseAttrInfoList = managerService.attrInfoList(
                category1Id,category2Id,category3Id
        );
        return Result.ok(baseAttrInfoList);
    }
    @GetMapping("saveAttrInfo")
    public Result saveAttrInfo(@PathVariable BaseAttrInfo baseAttrInfo){
        managerService.saveAttrInfo(baseAttrInfo);
        return Result.ok();

    }
    //分页
    @GetMapping("/baseTrademark/{page}/{limit}")
    public Result baseTrademark(@PathVariable(name = "page") Integer page,
                                @PathVariable(name = "limit") Integer limit){
       IPage iPage =  managerService.baseTrademark(page,limit);
       return Result.ok(iPage);

    }
    //查询品牌集合
    @GetMapping("/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
            List<BaseTrademark> baseTrademarkList = managerService.getTrademarkList();
            return Result.ok(baseTrademarkList);
    }

    //修改
    @GetMapping("getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId") Long attrId) {
        BaseAttrInfo baseAttrInfo = managerService.getAttrInfo(attrId);

        List<BaseAttrValue> baseAttrValueList = baseAttrInfo.getAttrValueList();
        return Result.ok(baseAttrValueList);
    }

    //spu 保存数据
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }


    //查询分页的结果 spu信息
    @GetMapping("/{page}/{limit}")
    public Result spuInfoPage(@PathVariable(name = "page")Long page,
                              @PathVariable(name = "limit") Long limit,
                              Long category3Id){
        IPage<SpuInfo> iPage = managerService.spuInfoPage(page,limit,category3Id);
        return Result.ok(iPage);
    }
    //spu id 查询所以图片
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId")Long spuId){
        List<SpuImage> spuImageList = managerService.spuImageList(spuId);
        return Result.ok(spuImageList);

    }
    //根据spuId查询销售属性及属性值
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = managerService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }
    //保存SKU
    @GetMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuinfo){
        managerService.saveSkuInfo(skuinfo);
        return Result.ok();

    }
    //查询所有SKuINfo的数据
    @GetMapping("/list/{page}/{limit}")
    public Result list(@PathVariable(name = "page")Long page,
                       @PathVariable(name = "limit")Long limit){
        IPage<SkuInfo> iPage = managerService.list(page,limit);
        return Result.ok(iPage);

    }
    //上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId") Long skuId){
        managerService.onSale(skuId);
        return Result.ok();
    }
    //下架
    @GetMapping("/cancelSale/{skuId}")
        public Result cancelSale(@PathVariable(name = "skuId") Long skuId){
        managerService.cancelSale(skuId);
        return Result.ok();
    }
}
